<?php


namespace App\Http\Repositories\Admin;


use App\Http\Repositories\BaseRepository;
use App\Http\Models\Admin\ArticleModel;
use Illuminate\Support\Facades\Redis;

class RecommendRepository extends BaseRepository
{
    private $article;
    public function __construct(ArticleModel $articleModel)
    {
        $this->article = $articleModel;
    }


    public function getRecommendList($data)
    {
        $where = [];
        isset($data['type']) && $where['article_type'] = $data['type'];

        $article = $this->article->getListByPage($where, ['*'], config('limit'));

        return $article;
    }

    public function addRecommend($data)
    {
        $title = '我爱PHP（第 %s 章）';
        $content = '我爱PHP（第 %s 张）';
        $describe = '我爱PHP（第 %s 张）';
        $cover = '%s,我没有封面也很帅';

        for ($i = 0; $i < 5; $i ++) {

            $data['title'] = sprintf($title, $i);
            $data['content'] = sprintf($content, $i);
            $data['describe'] = sprintf($describe, $i);
            $data['cover'] = sprintf($cover, $i);
            $data['article_type'] = rand(1, 5);

            $res = $this->article->create($data)->toArray();
            $array = [
                'article_id' => $res['id'],
                'praise_count' => 0,
                'despise_count' => 0,
                'favorites_count' => 0,
                'score' => 0,
                'create_time' => strtotime($res['create_time']),
            ];
            Redis::hMset(self::KEY.$res['id'].':count' , $array);
        }

        return [];
    }

    public function addPraise($data)
    {
        $key = self::KEY.$data['article_id'];

//        Redis::set($key.'_praise', 0);
        Redis::incr($key.'_praise');

        dd(Redis::get($key.'_praise'));

        if (Redis::sIsMember($key, $data['uuid'])) {
            Redis::sRem($key, $data['uuid']);
            Redis::decr($key.'_praise');
        } else {
            Redis::sAdd($key, $data['uuid']);
            Redis::incr($key.'_praise');
        }
//        dump(Redis::sMembers($key));
        return Redis::sMembers($key);
    }


    const KEY = 'blog:article:';
    const ARRAY_KEY = [
        1 => 'praise_count',
        2 => 'despise_count',
        3 => 'favorites_count',
    ];
    //分数
    const SCORE = [
        'browse_score' => 1,
        'praise_count' => 3,
        'despise_count' => -2,
        'favorites_count' => 5,
//        'day' => -10,
//        'score' => 0,
    ];
    const REDIS_KEY = [
        'article_id',
        'praise_count',
        'despise_count',
        'favorites_count',
        'score',
        'create_time'
    ];

    /**
     * 计算得分
     * @param $data
     * @return float|int
     */
    public function scoreCount($data)
    {
        $score = 0;

        foreach (self::SCORE as $k => $v) {
            $score += $v * ($data[$k] ?? 0);

        }
        return $score;
    }

    /**
     * 合计统计
     * @param $data
     * @return mixed
     */
    public function addCount($data)
    {
        $this->article->test();
        exit();
        $key = self::KEY.$data['article_id'].':'.self::ARRAY_KEY[$data['type']];

        if (Redis::sIsmember($key, $data['member_uuid'])) {
            Redis::sRem($key, $data['member_uuid']);
            $this->article->addCount(self::ARRAY_KEY[$data['type']], $data['article_id'], false);
            Redis::hIncrBy(self::KEY.$data['article_id'].':count', self::ARRAY_KEY[$data['type']], -1);
            Redis::hIncrBy(self::KEY.$data['article_id'].':count', 'score', self::SCORE[self::ARRAY_KEY[$data['type']]]);
        } else {
            Redis::sAdd($key, $data['member_uuid']);
            $this->article->addCount(self::ARRAY_KEY[$data['type']], $data['article_id'], true);
            Redis::hIncrBy(self::KEY.$data['article_id'].':count', self::ARRAY_KEY[$data['type']], 1);
            Redis::hIncrBy(self::KEY.$data['article_id'].':count', 'score', self::SCORE[self::ARRAY_KEY[$data['type']]]);
        }

        return Redis::hGetAll(self::KEY.$data['article_id'].':count');
    }


}
